package com.ludum.game.player;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.ludum.game.collision.CollisionManager;
import com.ludum.game.collision.RectangleShape;
import com.ludum.game.configuration.ConfigManager;
import com.ludum.game.rendering.Drawable;
import com.ludum.game.rendering.TextureManager;
import com.ludum.game.player.PlayerState;

public class Player implements Drawable {
	float orientation;
	
	Vector2 position;
	Vector2 speed;
	Vector2 acceleration;
	Vector2 size;
	RectangleShape shape;
	
	boolean hasMoved;
	
	/* Drawing variables */
	PlayerState state;		
	int frameGunter;
	
	public Player(Vector2 pos, float ori) {
		position     = pos;
		speed        = new Vector2(0,0);
		orientation  = ori;
		acceleration = new Vector2(0,orientation * ConfigManager.gravity);
		size = new Vector2(ConfigManager.playerSizeX,ConfigManager.playerSizeY);
		CollisionManager collisionManager = CollisionManager.getInstance();
		shape = collisionManager.newRectangleShape(pos, size);
	}
	
	public void moveRight() {
		acceleration.x = ConfigManager.acceleration;
		hasMoved = true;
		if(state == PlayerState.STANDING){
			state = PlayerState.RUNNING;
			frameGunter = 0;
		}
	}
	
	public void moveLeft() {
		acceleration.x = -ConfigManager.acceleration;
		hasMoved = true;
		if(state == PlayerState.STANDING){
			state = PlayerState.RUNNING;
			frameGunter = 0;
		}
	}
	
	/**
	 * This method should be called when the player
	 * collide vertically with a block
	 */
	public void downCollision(){
		acceleration.y = 0f;
		speed.y = 0f;
		state = PlayerState.STANDING;
		frameGunter = 0;
	}
	
	public void jump() {
		
		speed.y = -orientation * ConfigManager.jumpSpeed;
		if(state != PlayerState.JUMPING){
		}
	}
	
	public void stand() {
		hasMoved = false;
	}
	
	public void update(float dt) {
		speed.mulAdd( acceleration, dt );
		
		if ( !hasMoved ){
			if( state != PlayerState.JUMPING ){ 
				state = PlayerState.STANDING;
			}
			acceleration.x = 0;		
			speed.x *= ConfigManager.friction;
		}
		
		if ( orientation * speed.y > ConfigManager.maxVerticalSpeed ){
			speed.y = Math.signum(speed.y) * ConfigManager.maxVerticalSpeed;
		}
		if ( Math.abs(speed.x) > ConfigManager.maxSpeed ){
			speed.x = Math.signum(speed.x) * ConfigManager.maxSpeed;
		}
		else if ( Math.abs(speed.x) < ConfigManager.minSpeed && !hasMoved){
			speed.x = 0;			
			state = PlayerState.STANDING;
		}
		
		Vector2 displacement = speed.cpy();
		displacement.scl(dt);
		CollisionManager collisionManager = CollisionManager.getInstance();
		Vector2 newDisplacement = collisionManager.isInCollision(shape, displacement);

		position.add(newDisplacement);
		
		shape.updatePosition(position, size);
		frameGunter++;
	}
	
	public Vector2 getPosition() {
		return position;
	}
	
	@Override
	public void draw(SpriteBatch batch) {
		if ( state == PlayerState.JUMPING ) {
			//drawPlayerJump			
		}
		else if ( state == PlayerState.RUNNING ) {
			//drawPlayerRun			
		}
		else {
			//drawPlayerStand			
		}
		
		batch.draw(TextureManager.getInstance().playerTexture(0), position.x, position.y, size.x, size.y);
	}	

}
